/**
 * RAF.java
 *
 * This program demonstrates using the RandomAccessFile api
 * 
 * A random access file permits reads/writes to specific byte
 * positions in a file. The seek() method permits moving to
 * specific potions of the file to read or write. 
 * Note this also allows a file to have "holes" by seeking to
 * position N and then writing data. Bytes (0 ... N-1) are empty
 * (i.e. are a hole.)
 *
 * Usage:
 *	java RAF <integer value>
 */

import java.io.*;

public class RAF
{
	public static void main(String[] args) throws java.io.IOException {
//		if (args.length != 1) {
//			System.err.println("Usage: java RAF <integer value>");
//			System.exit(0);
//		}

		// the file representing the simulated  disk
     	File fileName;
     	RandomAccessFile disk = null;
		int val;

		//int number = Integer.parseInt(args[0]);
int number = 1024;
		try {
               fileName = new File("BACKING_STORE");
               disk = new RandomAccessFile(fileName, "rw");

			
			/**
			 * read() returns a byte, but since bytes
			 * in Java are signed, we use an integer
			 * to store its value to obtain the signed
			 * value of the byte.
			 */
						
			// seek again to byte position 100
			disk.seek(255);

			// now read 4 bytes
			byte[] data = new byte[256];
			disk.read(data);
			for (int i = 0; i < 256; i++) 
				System.out.println("byte " + i + ": " + data[i]);
          }
          catch (IOException e) {
               System.err.println ("Unable to start the disk");
               System.exit(1);
          }
		finally {
			disk.close();
		}
	}
}
